#!/bin/bash

# Import "_include.sh" to include other shared scripts
PGD=`echo $0 | awk -F '/' '{for (i=1;i<NF;i++) printf("%s/", $i); printf("\n")}'`
. $PGD/_include.sh

# Import shared scripts
_include "detect.sh"
_include "check.sh"
DISTRO=`linux_distro`

_assert_range "InputParams" $# 1
classname="$1"

MOFDIR="./mofs/"

_assert_info_off

function get_base_class_name()
{
    clsfn="$1"
    cat $clsfn | awk '($1 ~ /^class$/)&&($3 == ":")&&($5 == "{"){print $4}'
}

function print_class_prop()
{
    clsfn="$1"
    cat  $clsfn | awk 'BEGIN{tmp=""; desc=""; valmap=[]; vals=[]}{\
        if ($0 ~ /.*Description \(/) tmp=" ";\
        if ($0 ~ /\"\)/) desc=tmp;\
        if ($0 ~ /.+[a-zA-Z0-9\]];$/) printf("%s %s\t\n", $0, desc);\
    }'
    #cat  $clsfn | awk '/.+[a-zA-Z0-9\]];$/{printf("%s\t\n", $0)}'

}

# Print all property list in short format
echo "Property definations in MOF files:"
while [ 1 ]
do
    # Only care the first class file
    classfile=`find $MOFDIR | grep "$classname\.mof" | head -n 1 | awk '{print $1}'`
    _assert_non_empty "classfile" "$classfile" 

    #echo $classfile
    print_class_prop $classfile

    classname=`get_base_class_name $classfile`
    if [ -z "$classname" ]
    then
        break
    fi
done

exit 0


if [ "$DISTRO" == "sles11" ]
then
    exit 0
fi
echo ""
echo "Property defination in CIM repository"
classname="$1"
classfile=`find $REPO | grep "$classname\."`
echo $classfile
_assert_non_empty "classfile" "$classfile"

cat $classfile | awk '/^<PROPERTY.*/{\
    suffix="";\
    if ( $1 ~ /^<PROPERTY.ARRAY$/ ) suffix="[]";\
    name="";\
    type="";\
    class="";\
    for (i = 2; i<= NF; i++)\
    {\
        gsub("\"", "", $i);\
        gsub(">", "", $i);\
        split($i, arr, "=");\
        if (length(arr)==2)\
        {\
            if (arr[1]=="NAME") name=arr[2];\
            if (arr[1]=="TYPE") type=arr[2];\
            if (arr[1]=="CLASSORIGIN") class=arr[2];\
        }\
    }\
    printf("    %s %s%s\t :: %s\n", type, name, suffix, class);\
}'
